﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Research.Kinect.Nui;
using System.Windows;

namespace Kinect
{
    public partial class KinectController
    {
        public delegate void playerInPositionEventHandler(object sender);
        public playerInPositionEventHandler InthePosition;

        public bool GetPlayerPostion = false;//To turn on the engine

        //public float positiongap = 0.15f;

        //private float[] playerPosition= new float[3];
       
        //private float[] templatePosition={float.PositiveInfinity,float.PositiveInfinity,float.PositiveInfinity};
        float templatePosition =  float.PositiveInfinity;
        float[] playerPosition= {float.PositiveInfinity,float.PositiveInfinity};

        int cccc = 0;
        public void setPlayerPosition(Joint e)
        {
            playerPosition[cccc] = e.Position.X;
            cccc = (cccc + 1) % 2;
            //playerPosition[1]= e.Position.Y;
            //playerPosition[2] = e.Position.Z;
        }

        private void settemplate(Joint e)
        {
            //templatePosition[0] = e.Position.X;
            //templatePosition[1] = e.Position.Y;
            //templatePosition[2] = e.Position.Z;
            templatePosition=e.Position.X;

            GFileWriter writer = new Kinect.GFileWriter();
            writer.open("e:/GGBadguy.txt");
            writer.writeln(templatePosition);
            //for (int i = 0; i < 3; i++)
            //    writer.writeln(templatePosition[i]);        
            writer.close();
        }

        public void SetPosition()
        {
            int h =0;
            SkeletonFrame x;
            do
            {
                x = nui.SkeletonEngine.GetNextFrame(h);
                h+=20;
            } while (x == null);
            foreach(SkeletonData data in x.Skeletons)
            {
                if (data.TrackingState == SkeletonTrackingState.Tracked)
                {
                    settemplate(data.Joints[JointID.Spine]);
                    MessageBox.Show("Get your position");
                }
            }
        }

        public void CheckIfInThePosition()
        {
            if (templatePosition == float.PositiveInfinity)
                return;
            if(playerPosition[1]==float.PositiveInfinity)
                return;
            //if (Distance() - positiongap < 0)
            if((templatePosition-playerPosition[0])*(templatePosition-playerPosition[1])<0)
            {
                GetPlayerPostion = false;
                InthePosition(this);
            }
                
        }

        //private double Distance()
        //{
        //    double d = 0;
        //    for(int i = 0;i<3;i++)
        //    {
        //        d += Math.Pow(playerPosition[i] - templatePosition[i], 2);      
        //    }
        //    return Math.Sqrt(d);
        //}


    }
}
